package com.wontube.smr.remote;

import java.util.List;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.sites.analizers.IVideoAnalizer;
import com.wontube.smr.bo.sites.analizers.YouTubeVideoAnalizer;
import com.wontube.smr.bo.sites.youtube.feed.YouTubeFeedsBO;
import com.wontube.smr.client.IShortFilmHandler;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.client.model.vo.VideoFiles;
import com.wontube.smr.dao.bo.GenreSelectDao;
import com.wontube.smr.model.vo.YoutubeFeedParams;
import com.wontube.smr.pub.AppLoader;


@NoLoginSupport
public class ShortFilmHandler extends BasicHandler implements IShortFilmHandler {	
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> selectAllGenres() {
		return GenreSelectDao.selectGenres(Genre.CATE_SHORT_FILM);
	}
	
	@Override
	@CacheSupport(timeToIdleSeconds = 3600, timeToLiveSeconds = 3600)
	public VideoFiles getAllVideos(String url) {
		VideoFiles videoFiles = new VideoFiles();
		List<IVideoAnalizer> videoAnalizers = AppLoader.videoAnalizers;
		for (IVideoAnalizer analizer : videoAnalizers) {
			if (analizer.canProcess(url)) {
				analizer.getAllVideos(super.getContext(), videoFiles, url);
				return videoFiles;
			}
		}

		super.getContext().error("Please Enter the Valid URL.");
		return videoFiles;
	}

	@Override
	@CacheSupport(timeToIdleSeconds = 3600, timeToLiveSeconds = 3600)
	public VideoFiles getYouTubeVideos(String url) {
		VideoFiles videoFiles = new VideoFiles();
		YouTubeVideoAnalizer analizer = new YouTubeVideoAnalizer();
		analizer.getAllVideos(super.getContext(), videoFiles, url);

		return videoFiles;
	}
	
	@Override
	@CacheSupport
	public Movie getShortFilm(String filmId) {
		if (StringUtils.isNullOrEmpty(filmId)) {
			return null;
		}
		
		if (filmId.startsWith("yt")) {
			filmId = filmId.substring("yt".length());
		}
		if("undefined".equals(filmId)){
			return null;
		}
				
		return YouTubeFeedsBO.getSingle(filmId);
	}
	
	@Override
	@CacheSupport
	public MovieItems listRelated(String filmId, PagenateInfo pageInfo) {
		if (StringUtils.isNullOrEmpty(filmId)) {
			return null;
		}
		
		if (filmId.startsWith("yt")) {
			filmId = filmId.substring("yt".length());
		}
		return YouTubeFeedsBO.listRelated(filmId, pageInfo);
	}

	@Override
	@CacheSupport
	public MovieItems searchYouTube(String query, PagenateInfo pageInfo) {
		return YouTubeFeedsBO.searchVideoFeeds(query, null, pageInfo);		
	}	

	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 3600)
	public MovieItems searchVideos(PagenateInfo pageInfo) {
		return searchVideos(null, pageInfo);
	}

	@Override
	@CacheSupport
	public MovieItems searchVideos(String keyword, PagenateInfo pageInfo) {
		return searchVideos(keyword, null, pageInfo);
	}

	@Override
	@CacheSupport
	public MovieItems searchVideos(String keyword, String category, PagenateInfo pageInfo) {
		return YouTubeFeedsBO.searchVideoFeeds(keyword, category, pageInfo);			
	}	
	
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 3600)
	public MovieItems listPopular(String time, PagenateInfo pageInfo) {
		return listPopular(null, time, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 3600)
	public MovieItems listPopular(String genreName, String time, PagenateInfo pageInfo) {
		return YouTubeFeedsBO.selectStandardFeeds(YoutubeFeedParams.STANDARD_FEED_MOST_POPULAR,
				time, genreName, pageInfo);		
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listRecent(PagenateInfo pageInfo) {
		return listRecent(null, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listRecent(String genreName, PagenateInfo pageInfo) {
		return YouTubeFeedsBO.selectStandardFeeds(YoutubeFeedParams.STANDARD_FEED_MOST_RECENT,
				null, genreName, pageInfo);	
	}	
	
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 3600)
	public MovieItems listTopRated(String time, PagenateInfo pageInfo) {
		return listTopRated(time, null, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 3600)
	public MovieItems listTopRated(String time, String genreName, PagenateInfo pageInfo) {
		return YouTubeFeedsBO.selectStandardFeeds(YoutubeFeedParams.STANDARD_FEED_TOP_RATED,
				time, genreName, pageInfo);		
	}
}
